{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6fca13bb-7ac3-46f3-a941-791f94631c94",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install catboost xgboost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d8693c8e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-10-07T14:42:18.770904Z",
     "start_time": "2022-10-07T14:42:10.515802Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import lightgbm as lgb\n",
    "from datetime import datetime,timedelta\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.metrics import roc_auc_score,log_loss\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "import warnings\n",
    "from catboost import CatBoostClassifier\n",
    "from xgboost import XGBClassifier\n",
    "from lightgbm import LGBMClassifier\n",
    "warnings.filterwarnings('ignore')\n",
    "pd.set_option('display.max_columns',100)\n",
    "pd.set_option('display.max_rows',100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0703942b-5755-4fb5-ac57-4b85ca007472",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1_数据读取_B榜\t\t   B榜结果.csv\t     test.csv\n",
      "2_模型训练_B榜_lgb.ipynb   fea\t\t     train.csv\n",
      "3_模型训练_B榜_cbt.ipynb   pred_89129_61618  说明文档_B榜.pdf\n",
      "4_模型训练_B榜_xgb.ipynb   pred_89323_61785\n",
      "5_模型融合_B榜_vote.ipynb  pred_89479_61914\n"
     ]
    }
   ],
   "source": [
    "!ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f00ad08f-ba98-476f-b9e8-6632efb0f4e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train = pd.read_csv('train.csv')\n",
    "X_test = pd.read_csv('test.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0d00fa7a-a013-48ad-81b3-f74929ac35ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "y = X_train['Flag']\n",
    "X_train = X_train.drop(['CUST_NO', 'Flag'],axis=1)\n",
    "X_test = X_test.drop(['CUST_NO'],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9c614ccb-8d43-40b2-a46a-be8c916a3f1b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['NTRL_CUST_SEX_CD', 'NTRL_CUST_AGE', 'NTRL_RANK_CD', 'AST_DAY_FA_BAL',\n",
       "       'AST_MAVER_FA_BAL', 'AST_SAVER_FA_BAL', 'AST_YAVER_FA_BAL',\n",
       "       'AST_DAY_AUM_BAL', 'AST_MAVER_AUM_BAL', 'AST_SAVER_AUM_BAL',\n",
       "       ...\n",
       "       'IBTF_YEAR_TR_CNT_IN', 'TPAY_MOTH_TR_AMT', 'TPAY_SEAN_TR_AMT',\n",
       "       'TPAY_MOTH_NET_TR_AMT', 'TPAY_SEAN_NET_TR_AMT', 'TPAY_MOTH_TR_CNT',\n",
       "       'TPAY_SEAN_TR_CNT', 'IBTF_TPAY_MOTH_TR_AMT',\n",
       "       'IBTF_TPAY_MOTH_NET_TR_AMT', 'IBTF_TPAY_MOTH_TR_CNT'],\n",
       "      dtype='object', length=368)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8321d4de",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-10-07T14:42:27.645043Z",
     "start_time": "2022-10-07T14:42:27.616989Z"
    }
   },
   "outputs": [],
   "source": [
    "# Some useful parameters which will come in handy later on\n",
    "ntrain = X_train.shape[0]\n",
    "ntest = X_test.shape[0]\n",
    "SEED = 2019 # for reproducibility\n",
    "NFOLDS = 5 # set folds for out-of-fold prediction\n",
    "kf = StratifiedKFold(n_splits= NFOLDS, random_state=SEED,shuffle=True)\n",
    "\n",
    "# Class to extend the Sklearn classifier\n",
    "class SklearnHelper(object):\n",
    "    def __init__(self, clf, seed=0, params=None):\n",
    "        params['random_state'] = seed\n",
    "        self.clf = clf(**params)\n",
    "\n",
    "    def train(self, x_train, y_train, x_valid, y_valid):\n",
    "        self.clf.fit(x_train, y_train,eval_set=[(x_valid, y_valid)],\n",
    "                     early_stopping_rounds=500,verbose=200)\n",
    "\n",
    "    def predict(self, x):\n",
    "        return self.clf.predict_proba(x)[:,1]\n",
    "    \n",
    "    def fit(self,x_train,y_train, x_valid,y_valid):\n",
    "        return self.clf.fit(x,y,eval_set=[(x_valid, y_valid)],early_stopping_rounds=500)\n",
    "    \n",
    "    def feature_importances(self,x,y):\n",
    "        print(self.clf.fit(x,y).feature_importances_)\n",
    "    \n",
    "# Class to extend XGboost classifer\n",
    "\n",
    "\n",
    "\n",
    "def get_oof(clf, x_train, y_train, x_test):\n",
    "    oof_train = np.zeros((ntrain,))\n",
    "    oof_test = np.zeros((ntest,))\n",
    "    oof_test_skf = np.empty((NFOLDS, ntest))\n",
    "\n",
    "    for i, (train_index, test_index) in enumerate(kf.split(x_train,y)):\n",
    "        x_tr = x_train[train_index]\n",
    "        y_tr = y_train[train_index]\n",
    "        x_te = x_train[test_index]\n",
    "        y_te = y_train[test_index]\n",
    "        \n",
    "        clf.train(x_tr, y_tr,x_te,y_te)\n",
    "\n",
    "        oof_train[test_index] = clf.predict(x_te)\n",
    "        oof_test_skf[i, :] = clf.predict(x_test)\n",
    "\n",
    "    oof_test[:] = oof_test_skf.mean(axis=0)\n",
    "    return oof_train.reshape(-1, 1), oof_test.reshape(-1, 1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1c87faa1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-10-07T14:42:27.676741Z",
     "start_time": "2022-10-07T14:42:27.647720Z"
    }
   },
   "outputs": [],
   "source": [
    "params_cbt = {'learning_rate': 0.03,\n",
    "            #  'task_type':'GPU',\n",
    "          'depth': 8,\n",
    "          'l2_leaf_reg': 10,\n",
    "          'bootstrap_type': 'Bernoulli',\n",
    "          'od_type': 'Iter',\n",
    "          'od_wait': 500,\n",
    "         'iterations':10000,\n",
    "              'random_state': 1024,\n",
    "         'verbose':100,\n",
    "         'eval_metric':'AUC',              \n",
    "          'allow_writing_files': False}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "72479455",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-10-07T14:56:50.259340Z",
     "start_time": "2022-10-07T14:42:27.680725Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0:\ttest: 0.8580991\tbest: 0.8580991 (0)\ttotal: 137ms\tremaining: 22m 50s\n",
      "200:\ttest: 0.8881387\tbest: 0.8881387 (200)\ttotal: 14.4s\tremaining: 11m 40s\n",
      "400:\ttest: 0.8896301\tbest: 0.8896561 (397)\ttotal: 28.3s\tremaining: 11m 17s\n",
      "600:\ttest: 0.8902274\tbest: 0.8903759 (549)\ttotal: 42.1s\tremaining: 10m 58s\n",
      "800:\ttest: 0.8901168\tbest: 0.8903759 (549)\ttotal: 56.2s\tremaining: 10m 45s\n",
      "1000:\ttest: 0.8894189\tbest: 0.8903759 (549)\ttotal: 1m 10s\tremaining: 10m 29s\n",
      "Stopped by overfitting detector  (500 iterations wait)\n",
      "\n",
      "bestTest = 0.8903758616\n",
      "bestIteration = 549\n",
      "\n",
      "Shrink model to first 550 iterations.\n",
      "0:\ttest: 0.8647779\tbest: 0.8647779 (0)\ttotal: 90.1ms\tremaining: 15m\n",
      "200:\ttest: 0.8955025\tbest: 0.8955025 (200)\ttotal: 14.2s\tremaining: 11m 31s\n",
      "400:\ttest: 0.8977615\tbest: 0.8977615 (400)\ttotal: 28.1s\tremaining: 11m 12s\n",
      "600:\ttest: 0.8980792\tbest: 0.8980890 (575)\ttotal: 42.1s\tremaining: 10m 57s\n",
      "800:\ttest: 0.8985067\tbest: 0.8986094 (778)\ttotal: 56.2s\tremaining: 10m 45s\n",
      "1000:\ttest: 0.8982328\tbest: 0.8986094 (778)\ttotal: 1m 10s\tremaining: 10m 30s\n",
      "1200:\ttest: 0.8981084\tbest: 0.8986094 (778)\ttotal: 1m 24s\tremaining: 10m 16s\n",
      "Stopped by overfitting detector  (500 iterations wait)\n",
      "\n",
      "bestTest = 0.8986093543\n",
      "bestIteration = 778\n",
      "\n",
      "Shrink model to first 779 iterations.\n",
      "0:\ttest: 0.8434767\tbest: 0.8434767 (0)\ttotal: 88.6ms\tremaining: 14m 46s\n",
      "200:\ttest: 0.9006734\tbest: 0.9007060 (195)\ttotal: 14.1s\tremaining: 11m 29s\n",
      "400:\ttest: 0.9018860\tbest: 0.9018896 (398)\ttotal: 28.1s\tremaining: 11m 13s\n",
      "600:\ttest: 0.9021000\tbest: 0.9021285 (521)\ttotal: 41.9s\tremaining: 10m 54s\n",
      "800:\ttest: 0.9017286\tbest: 0.9021285 (521)\ttotal: 55.8s\tremaining: 10m 40s\n",
      "1000:\ttest: 0.9016024\tbest: 0.9021285 (521)\ttotal: 1m 10s\tremaining: 10m 30s\n",
      "Stopped by overfitting detector  (500 iterations wait)\n",
      "\n",
      "bestTest = 0.9021284753\n",
      "bestIteration = 521\n",
      "\n",
      "Shrink model to first 522 iterations.\n",
      "0:\ttest: 0.8453714\tbest: 0.8453714 (0)\ttotal: 85.6ms\tremaining: 14m 15s\n",
      "200:\ttest: 0.8919937\tbest: 0.8919937 (200)\ttotal: 14.3s\tremaining: 11m 37s\n",
      "400:\ttest: 0.8930243\tbest: 0.8930289 (389)\ttotal: 28.1s\tremaining: 11m 12s\n",
      "600:\ttest: 0.8932790\tbest: 0.8933122 (591)\ttotal: 41.9s\tremaining: 10m 55s\n",
      "800:\ttest: 0.8933766\tbest: 0.8935216 (660)\ttotal: 55.8s\tremaining: 10m 40s\n",
      "1000:\ttest: 0.8933864\tbest: 0.8935216 (660)\ttotal: 1m 9s\tremaining: 10m 26s\n",
      "Stopped by overfitting detector  (500 iterations wait)\n",
      "\n",
      "bestTest = 0.8935216045\n",
      "bestIteration = 660\n",
      "\n",
      "Shrink model to first 661 iterations.\n",
      "0:\ttest: 0.8514874\tbest: 0.8514874 (0)\ttotal: 86.3ms\tremaining: 14m 23s\n",
      "200:\ttest: 0.8885823\tbest: 0.8885823 (200)\ttotal: 14.3s\tremaining: 11m 37s\n",
      "400:\ttest: 0.8894481\tbest: 0.8896017 (318)\ttotal: 28.4s\tremaining: 11m 18s\n",
      "600:\ttest: 0.8901527\tbest: 0.8901893 (587)\ttotal: 42.3s\tremaining: 11m 1s\n",
      "800:\ttest: 0.8901197\tbest: 0.8902491 (647)\ttotal: 56.2s\tremaining: 10m 45s\n",
      "1000:\ttest: 0.8900037\tbest: 0.8902491 (647)\ttotal: 1m 10s\tremaining: 10m 33s\n",
      "Stopped by overfitting detector  (500 iterations wait)\n",
      "\n",
      "bestTest = 0.8902491442\n",
      "bestIteration = 647\n",
      "\n",
      "Shrink model to first 648 iterations.\n"
     ]
    }
   ],
   "source": [
    "clf_cbt = SklearnHelper(clf=CatBoostClassifier, seed=1024, params=params_cbt)\n",
    "oof_cbt, test_cbt = get_oof(clf_cbt,X_train.values,y,X_test.values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6dedd240",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-10-07T14:56:57.678856Z",
     "start_time": "2022-10-07T14:56:57.655868Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.8947937840388333\n"
     ]
    }
   ],
   "source": [
    "auc = roc_auc_score(y,oof_cbt)\n",
    "print(roc_auc_score(y,oof_cbt))  # 0.8947937840388333"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3e7780f-7226-413a-b995-aadba29701a9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "670bd9cf-2f2a-4851-8770-08ba91db15fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "oof_stack = oof_cbt\n",
    "predictions_stack = test_cbt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a32c921a-82b9-45cc-bfe4-69da03dad3fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "#np.min(oof_stack),np.max(oof_cbt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "3396a851-8d18-4e86-a296-7eec6b417482",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import f1_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ece1960c-11be-4858-adbb-af71b4880148",
   "metadata": {},
   "outputs": [],
   "source": [
    "thresholds = np.arange(0,1,0.01)\n",
    "f1_scores = []\n",
    "for x in thresholds:\n",
    "    pred_label = np.where(oof_stack>x,1,0)\n",
    "    score = f1_score(y,pred_label)\n",
    "    f1_scores.append(score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "224aef7a-d929-4066-a784-0e87849e6815",
   "metadata": {},
   "outputs": [],
   "source": [
    "best_score = np.max(f1_scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "bcba8fb3-9d0f-4134-a06c-3544c5d6d866",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.6191414496833216\n"
     ]
    }
   ],
   "source": [
    "print(np.max(f1_scores))  # 0.6191414496833216"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3b396fe4-d76c-4dff-9117-228d9e95a61e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([3642.,  524.,  219.,  135.,  106.,   75.,   90.,   83.,   79.,\n",
       "          71.]),\n",
       " array([0.0059852 , 0.10344178, 0.20089836, 0.29835494, 0.39581151,\n",
       "        0.49326809, 0.59072467, 0.68818125, 0.78563782, 0.8830944 ,\n",
       "        0.98055098]),\n",
       " <BarContainer object of 10 artists>)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAD4CAYAAAAAczaOAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy89olMNAAAACXBIWXMAAAsTAAALEwEAmpwYAAASv0lEQVR4nO3df6zd9X3f8ecr5ke6JSumvkWe7c2sc9Q5mWrQHVB12mhYwFApTtUuMlKLi9DcdTC1W1SVdH+QJkNKtCVISCmdI9xA1YawtF2s1i3zCBXKNH5cGuJgKOMWSLHn4NuY0EaobJD3/jgf2lN6r++51+eey/Xn+ZCO7vf7/n6+3/P5+Fqv8z2f7/ecm6pCktSHt612ByRJk2PoS1JHDH1J6oihL0kdMfQlqSNnrXYHTmXDhg21devW1e6GJK0pjz322J9W1dR8297Sob9161ZmZmZWuxuStKYk+fpC25zekaSOGPqS1BFDX5I6YuhLUkcMfUnqiKEvSR0x9CWpI4a+JHXE0JekjrylP5F7urbe/Lur8rzPf/xHVuV5JWkxnulLUkcMfUnqiKEvSR0x9CWpI4uGfpK3J3kkyVeTHEnyS63+2STPJXm8PXa0epLcnmQ2yeEkFw8da0+SZ9pjz4qNSpI0r1Hu3nkVeG9VfTvJ2cCXk/xe2/bzVfWFN7W/GtjWHpcCdwCXJjkfuAWYBgp4LMmBqnppHAORJC1u0TP9Gvh2Wz27PeoUu+wC7m77PQScl2QjcBVwqKpOtqA/BOw8ve5LkpZipDn9JOuSPA6cYBDcD7dNt7YpnNuSnNtqm4AXhnY/2moL1d/8XHuTzCSZmZubW9poJEmnNFLoV9XrVbUD2AxckuQ9wIeB7wf+CXA+8Avj6FBV7auq6aqanpqa9088SpKWaUl371TVt4AHgJ1VdbxN4bwK/CpwSWt2DNgytNvmVluoLkmakFHu3plKcl5b/i7g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yQbGEz3PDvBPq6EUcb9J8AVAEn+EYPQn5toL1fHAeC6dhfPZcDLVXV8lB3X7PROLfCVDkk+CsxU1QHgTgZv92YZXBTZvXo9Ho8Rx/2fgHcA/7Vdt/6Tqnr/qnV6DEYc9xlpxLHfB1yZ5EngdeDnq2pNv6sdcdwfAj6T5N8xuKj7U2fAiR1JPsfgRXxDu15xC3A2QFX9CoPrF9cAs8ArwPUjH/sM+PeRJI1oLU/vSJKWyNCXpI4Y+pLUEUNfkjpi6EtSRwx9SeqIoS9JHfn/meJ73NArCQUAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.hist(predictions_stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "cffb24ca-c1b0-42cd-ba00-4a778b13f1cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "best_threshold = thresholds[np.argmax(f1_scores)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "838adff0-f73c-4dec-9611-c066b0eedf61",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.31\n"
     ]
    }
   ],
   "source": [
    "print(best_threshold)  # 0.31"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "6621f610-3981-47e8-a9cc-db08926f54d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "pred = np.where(predictions_stack>best_threshold,1,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "743d27ea-f198-4796-b92a-14c5b6db92e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "1498802e-86e0-4fe5-8e30-2a0838ba234a",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_path = '../../contest/train'\n",
    "stage_path = '../../contest/B榜'\n",
    "stage = 'B'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e1d2aa67-5d19-4433-968b-9fed04099638",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_test = pd.read_csv(os.path.join(stage_path,f'DZ_TARGET_TESTB.csv'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b89b57fa-a1e6-4d25-a145-4d2498541c86",
   "metadata": {},
   "outputs": [],
   "source": [
    "pred = pred.reshape(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "9235ac4e-99aa-42d1-92e4-4ece05fd4203",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = pd.DataFrame({'CUST_NO':df_test.cust_no,'Flag':pred})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "1aaf667f-12f4-460d-94fc-a5ab76b3291f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    4406\n",
       "1     618\n",
       "Name: Flag, dtype: int64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.Flag.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "53b3517d-6dc0-45af-99e7-07d7cf8cd515",
   "metadata": {},
   "outputs": [],
   "source": [
    "result.to_csv('pred_{0}_{1}'.format(str(auc)[2:7], str(best_score)[2:7]),header=False,index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d623d6c-32a8-4a92-ad24-963edd59a96a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c55a771-2816-467d-9f5f-a31949376690",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
